package com.tosit.bmx.service.admin.Impl;

import com.tosit.bmx.domian.Area;
import com.tosit.bmx.domian.AreaUser;
import com.tosit.bmx.mapper.AreaMapper;
import com.tosit.bmx.mapper.AreaUserMapper;
import com.tosit.bmx.service.admin.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 区域实现类
 * @author tsl
 * @create 2018-04-22 16:13
 **/
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    AreaUserMapper areaUserMapper;

    @Override
    @Transactional
    public Integer addArea(Area area) {
        int i = areaMapper.insertSelective(area);
        
        return i;
    }

    @Override
    @Transactional
    public Integer removeArea(Integer id) {
        areaUserMapper.deleteByAreaId(id);
        int i = areaMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    @Transactional
    public Integer modifyArea(Area area) {
        int i = areaMapper.updateByPrimaryKeySelective(area);
        return i;
    }

    @Override
    public List<Area> gainArea() {
        return areaMapper.selectAll();
    }

    @Override
    @Transactional
    public Integer removeArea(List<Integer> ids) {
        int i = areaMapper.deleteByIds(ids);
        return i;
    }

    @Override
    @Transactional
    public Integer relateUser(List<AreaUser> userIds) {
        int i = areaMapper.insertRelate(userIds);
        return i;
    }

    @Override
    @Transactional
    public Integer deleteUser(List<Integer> auserIds) {
        int i = areaMapper.deleteRelate(auserIds);
        return i;
    }

    @Override
    @Transactional
    public Integer updateUser(List<AreaUser> areaUsers) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < areaUsers.size(); i++) {
            set.add(areaUsers.get(i).getUid());
        }
        List<Integer> userIds = new ArrayList<>();
        for (Integer id:set) {
            userIds.add(id);
        }
        Integer areaVos= deleteUser(userIds);
        return  relateUser(areaUsers);
    }
}
